﻿Imports DevExpress.XtraEditors

Public Class frmContact
    Dim data As DataTable
    Public Sub loadContact()
        Try
            data = mysql_query("SELECT * FROM tb_contact ORDER BY email ASC")
            GridControlList.DataSource = data
            If data.Rows.Count > 0 Then
                SimpleButtonEdit.Enabled = True
                SimpleButtonDelete.Enabled = True
            Else
                SimpleButtonEdit.Enabled = False
                SimpleButtonDelete.Enabled = False
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub frmContact_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        loadContact()
    End Sub

    Private Sub SimpleButtonAdd_Click(sender As Object, e As EventArgs) Handles SimpleButtonAdd.Click
        frmContact_Form.Dispose()
        frmContact_Form.ShowDialog()
    End Sub

    Private Sub SimpleButtonEdit_Click(sender As Object, e As EventArgs) Handles SimpleButtonEdit.Click
        Try
            frmContact_Form.Dispose()
            frmContact_Form.is_edit = True
            frmContact_Form.id = CType(GridViewList.GetFocusedRowCellDisplayText("id_kontak").ToString, Long)
            frmContact_Form.old_serverid = GridViewList.GetFocusedRowCellDisplayText("email").ToString
            frmContact_Form.ShowDialog()
        Catch ex As Exception
            XtraMessageBox.Show("Please Select One Item For Editing!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub SimpleButtonRefresh_Click(sender As Object, e As EventArgs) Handles SimpleButtonRefresh.Click
        loadContact()
    End Sub

    Private Sub SimpleButtonDelete_Click(sender As Object, e As EventArgs) Handles SimpleButtonDelete.Click
        If XtraMessageBox.Show("Are you sure want to delete this contact?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
            Cursor = Cursors.WaitCursor
            Dim id As String = GridViewList.GetFocusedRowCellDisplayText("id_kontak").ToString
            mysql_non_query(String.Format("DELETE FROM tb_contact WHERE id_kontak = '{0}'", id))
            loadContact()
            Cursor = Cursors.Default
        End If
    End Sub
End Class